/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * produto_log.java
 *
 * Created on 10/06/2011, 02:28:44
 */
package Visualização;

import java.text.ParseException;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Pedro
 */
public class produto_log extends javax.swing.JFrame {

    /** Creates new form produto_log */
    public produto_log() {
        initComponents();
        this.setLocationRelativeTo(null);
        int i;
        tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
        tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
        DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
        modelo.setNumRows(0); 
        for(i=0;i<7;i++)
            modelo.addRow(new Object [] {"","",""});
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        tituloLabel = new javax.swing.JLabel();
        codigoLabel = new javax.swing.JLabel();
        codigoInput = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaShow = new javax.swing.JTable();
        buscarButton = new javax.swing.JButton();
        dataInput = new javax.swing.JFormattedTextField();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        tituloLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        tituloLabel.setText("Movimentações");

        codigoLabel.setText("Código:");

        codigoInput.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                codigoInputActionPerformed(evt);
            }
        });

        jLabel1.setText("Data:");

        tabelaShow.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "ID", "Descrição", "Data"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabelaShow);

        buscarButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/Buscar.png"))); // NOI18N
        buscarButton.setText("Buscar");
        buscarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buscarButtonActionPerformed(evt);
            }
        });

        try {
            dataInput.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel2.setText("dd/MM/aaaa");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tituloLabel)
                        .addContainerGap(261, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(codigoLabel)
                            .addComponent(jLabel1))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(codigoInput, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                                .addGap(63, 63, 63))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(dataInput, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel2)
                                .addContainerGap())))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(buscarButton)
                        .addContainerGap(324, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 375, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tituloLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(codigoLabel)
                    .addComponent(codigoInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(dataInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buscarButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void codigoInputActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_codigoInputActionPerformed
        buscar();
}//GEN-LAST:event_codigoInputActionPerformed

    private void buscarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buscarButtonActionPerformed
        buscar();
}//GEN-LAST:event_buscarButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new produto_log().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buscarButton;
    private javax.swing.JTextField codigoInput;
    private javax.swing.JLabel codigoLabel;
    private javax.swing.JFormattedTextField dataInput;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tabelaShow;
    private javax.swing.JLabel tituloLabel;
    // End of variables declaration//GEN-END:variables
    public void buscar()
    {
        if (!codigoInput.getText().equals(""))
        {
            int tam, i, j=0;
            String mat[][] = new String[500][3];
            
            BO.produtoBO pBO = new BO.produtoBO();
            pBO.setIdBO(Integer.parseInt(codigoInput.getText()));
            
            Negocio.produto p = new Negocio.produto();
            p.BuscarPorCod(pBO);
            
            tam = pBO.getTam();
            mat = pBO.getMat();
            
            tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
            tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
            DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
            modelo.setNumRows(0); 
            for(i=0;i<tam;i++)
                modelo.addRow(new Object [] {mat[i][j],mat[i][j+1],mat[i][j+2]});
            
            if(modelo.getRowCount()==0)
            {
                tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
                tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
                tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
                DefaultTableModel modelo1 = (DefaultTableModel)tabelaShow.getModel();
                modelo.setNumRows(0); 
                for(i=0;i<7;i++)
                    modelo.addRow(new Object [] {"","",""});
                JOptionPane.showMessageDialog(rootPane, "Não há registro para este produto!");
            }
        }
        else
        {
            int tam, i, j=0;
            String mat[][] = new String[500][3];
            
            BO.produtoBO pBO = new BO.produtoBO();
            pBO.setDataBO(dataInput.getText());
            
            Negocio.produto p = new Negocio.produto();
            p.BuscarPorDat(pBO);
            
            tam = pBO.getTam();
            mat = pBO.getMat();
            
            tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
            tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
            tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
            DefaultTableModel modelo = (DefaultTableModel)tabelaShow.getModel();
            modelo.setNumRows(0); 
            for(i=0;i<tam;i++)
                modelo.addRow(new Object [] {mat[i][j],mat[i][j+1],mat[i][j+2]});
            
            if(modelo.getRowCount()==0)
            {
                tabelaShow.getColumnModel().getColumn(0).setPreferredWidth(30);
                tabelaShow.getColumnModel().getColumn(1).setPreferredWidth(150);
                tabelaShow.getColumnModel().getColumn(2).setPreferredWidth(30);
                DefaultTableModel modelo1 = (DefaultTableModel)tabelaShow.getModel();
                modelo.setNumRows(0); 
                for(i=0;i<7;i++)
                    modelo.addRow(new Object [] {"","",""});
                JOptionPane.showMessageDialog(rootPane, "Não há registro para este produto!");
            }
        }
        dataInput.setText("");
        codigoInput.setText("");
    }
}
